var EBEventDays = Class.create( );
EBEventDays.prototype = {

	initialize: function( ) {
		this.container = $($RGC().daysContainer);
		this.dayWidth = $RGC().eventDayWidth;
		
		var diffDays = parseInt($RGC().totalDays/2);
		var today = new Date;
		this.startDate = new Date;
		this.startDate.setDate(today.getDate() - diffDays);
		this.startDate.setHours(0);
		this.startDate.setMinutes(0);
		this.startDate.setSeconds(0);
		this.endDate = new Date;
		this.endDate.setDate(today.getDate() + diffDays - 1);
		this.endDate.setHours(0);
		this.endDate.setMinutes(0);
		this.endDate.setSeconds(0);
		$RGE('days-data').addObserver(this);
	},
	
	normalize: function(value) {
		value = value.toString();
		if(value.length == 1 ) return "0" + value;
		else return value;
	},
	
	getIdFromDate: function(d) {
		return "event-day-" + this.normalize(d.getDate()) + this.normalize(d.getMonth()+1) + d.getFullYear();
	},
	
	doBrowserPositioning: function(event) {
		$RGE('browser').doPositioningByDate(Event.element(event).getAttribute('title'));
	},
	
	start: function() {
		var currentDate = new Date( this.startDate );
		var leftPosition = 0;
		while(currentDate <= this.endDate) {
			//Generar los divs
	        var datec = document.createElement('div');
	        datec.setAttribute("class","event-day-slot");
	        datec.setAttribute("id",this.getIdFromDate(currentDate));
	        datec.setAttribute("style","left:"+leftPosition+"px; width:"+(this.dayWidth - 2)+"px;");
	        datec.setAttribute("title", currentDate.toString());
	        this.container.appendChild(datec);
	        Event.observe(datec, 'click', this.doBrowserPositioning.bind(this));
	        leftPosition += this.dayWidth;
	        currentDate.setDate(currentDate.getDate() + 1);
		}
		$RGE('days-data').setParams();
		$RGE('days-data').load();
	},
	
	update: function() {
		if(!$RGE('days-data').isLoaded( )) return;
		this.clear();
		$A($RGE('days-data').dates).each(
			function(el) {
				if($RGE('priority').level == el['priority']) this.setDate(el['date']);
			}.bind(this)
		);
		//$('txt').innerHTML = $RGE('priority').level;
	},
	
	setDate: function(d) {
		var el = $(this.getIdFromDate(d));
		Element.addClassName(el, 'event-day-filled');
		
	},
	
	clear: function( ) {
		var divs = this.container.getElementsByTagName( 'div' );
		if( ! divs.length ) return( false );
		$A( divs ).each( function(el) { Element.removeClassName(el, 'event-day-filled') } );
	}

}
